(function () {

    "use strict";
    var popup = new auiPopup();
    var api = {
        getMealsUrl: '/api/cash/getMeal',
        doCashInUrl: '/api/cash/cashIn'
    };

    sm.controller('wx-center-cashin-controller', ['$scope', '$http', 'Toast', 'Modal', function ($scope, $http, Toast, Modal){

        $scope.meals = [];
        $scope.selectedItem = {};
        $scope.isLoading = false;
        $scope.cashinType = 'system';
        $scope.limitAmount = {
            amount: 1000000,
            amount_format: '10000.00'
        };
        $scope.customData = {
            'use': false,
            'showError': false,
            'errorMsg': '',
            'amount': 0,
            'amount_format': ''
        };

        $scope.$watch('customData.amount_format', function (newValue, oldValue) {
            if (newValue === oldValue) {
                return false;
            }

            var amount = parseInt($scope.customData.amount_format * 100);
            if (amount < 0 || amount > $scope.limitAmount.amount) {
                $scope.customData.use = false;
                showError(true, '输入金额不符合要求');
            } else {
                showError(false, '');
            }
        });

        function showError(isShow, errMsg) {
            $scope.customData.showError = isShow;
            $scope.customData.errorMsg = errMsg;
        }

        $scope.getMeals = function () {
            $http.get(api.getMealsUrl, {
                params: {
                    page: 1
                }
            }).success(function(res) {
                if (res.state) {
                    $scope.meals = res.data.data;
                    if ($scope.meals.length) {
                        $scope.meals[0].selected = true;
                        $scope.selectedItem = $scope.meals[0];
                    }
                }
            });
        };
        $scope.getMeals();

        // 自定义充值金额
        $scope.customPanel = function() {
            $scope.cashinType = 'custom';
            popup.show(document.getElementById("customAmount"));
        };

        // 使用自定义金额
        $scope.useCustomAmount = function () {
            var amount = parseInt($scope.customData.amount_format * 100);
            console.log(amount);
            if (amount < 0 || amount > $scope.limitAmount.amount) {
                $scope.customData.use = false;
            } else {
                $scope.customData.use = true;
                $scope.customData.amount = amount;
                $scope.customData.amount_show = (amount / 100).toFixed(2);
                popup.hide();
            }
        };

        $scope.chooseItem = function(m) {
            var index = $scope.meals.indexOf(m);
            angular.forEach($scope.meals, function(val, k) {
                $scope.meals[k].selected = false;
            });
            $scope.meals[index].selected = true;
            $scope.selectedItem = $scope.meals[index];
        };

        // 充值
        $scope.doCashIn = function () {

            Modal.alert({
                title: "操作提示",
                content: "您确定选择该套餐?",
                buttons: [{
                    name: "放弃"
                }, {
                    name: "立即充值",
                    event: function () {
                        doCashIn();
                    }
                }]
            });
        };

        function doCashIn() {
            var data = {};
            if ($scope.cashinType == 'system' && !$scope.customData.use) {
                data.mealId = $scope.selectedItem.id;
                data.amount = $scope.selectedItem.amount;
            } else if ($scope.cashinType == 'custom' && $scope.customData.use) {
                data.amount = $scope.customData.amount;
            } else {
                return false;
            }

            data.cashinType = $scope.cashinType;

            $scope.isLoading = true;
            Toast.loading();
            $http.post(api.doCashInUrl, data).success(function (res) {
                Toast.stop();
                $scope.isLoading = false;
                if (res.state) {
                    var sdk = res.data.sdk;
                    wx.chooseWXPay({
                        timestamp: sdk.timeStamp,
                        nonceStr: sdk.nonceStr,
                        package: sdk.package,
                        signType: 'MD5',
                        paySign: sdk.paySign,
                        success: function (res) {
                            location.href = '/center/cash'
                        }
                    });
                } else {

                }
            });
        }

    }]);
})();

